package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.domain.GiteeAddress;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.GiteeAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/gitee")
public class GiteeController  extends BaseController {
    private String prefix = "system/giteeMap";
    @Autowired
    GiteeAddressService giteeAddressService;
    @GetMapping()
    public String config() {
        return prefix + "/gitee";
    }


    @RequiresPermissions("system:gitee:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GiteeAddress giteeAddress)
    {
        List<GiteeAddress> list = giteeAddressService.findList(giteeAddress);
        return getDataTable(list);
    }


    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @RequiresPermissions("system:gitee:remove")
    @RequestMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(giteeAddressService.delete(id));
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:gitee:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated GiteeAddress giteeAddress)
    {
        GiteeAddress gitee=new GiteeAddress();
        gitee.setAddress(giteeAddress.getAddress());
        List<GiteeAddress> list=giteeAddressService.findList(gitee);
        if(list!=null && list.size()>0){
            return giteeError();
        }
        giteeAddress.setId(UUID.randomUUID().toString());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(giteeAddressService.insert(giteeAddress));
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("system:gitee:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("giteeAddress", giteeAddressService.get(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:gitee:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated GiteeAddress giteeAddress)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(giteeAddressService.update(giteeAddress));
    }

    /**
     * 项目是否上线
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id ,String status)
    {
        GiteeAddress giteeAddress = giteeAddressService.get(id);
        giteeAddress.setLogged(status);
        return toAjax(giteeAddressService.update(giteeAddress));
    }


    /**
     * 是否为git项目
     */
    @PostMapping("/changeStatusGit")
    @ResponseBody
    public AjaxResult changeStatusGit(String id ,String status)
    {
        GiteeAddress giteeAddress = giteeAddressService.get(id);
        giteeAddress.setGitline(status);
        return toAjax(giteeAddressService.update(giteeAddress));
    }
}
